﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using MsgCenter.Service.Helper;
using MsgCenter.Service.BLL;
using Utility;
using Utility.Json;
using Utility.Model;

namespace MsgCenter.Service
{
    partial class MessageService : ServiceBase
    {
        public MessageService()
        {
            InitializeComponent();
            LogServiceHelper.SetConfig();
            AppConfigHelper.SetConfig();
            MsgBLLService.CreateDataService();
        }

        protected override void OnStart(string[] args)
        {
            isWC = true;
            StartMobileMsgService();
            StartDataSyncService();
            StartWeChatService();
        }

        private void StartMobileMsgService()
        {
            try
            {
                if (AppConfigHelper.strIsSmsSync == 1)
                {
                    LogServiceHelper.WriteMessageLog("开始启动短信服务。。。。。。。。");
                    timer1.Enabled = true;
                    timer1.Interval = (double)AppConfigHelper.strAlertTimer;
                    timer1.Start();
                    LogServiceHelper.WriteMessageLog("短信服务成功启动");
                }
                else
                {
                    LogServiceHelper.WriteMessageLog("短信不需要同步发送");
                }
            }
            catch (Exception ex)
            {
                LogServiceHelper.WriteMessageError("数据同步服务成功失败" + ex.Message, ex);
            }
        }

        private void StartDataSyncService()
        {
            try
            {
                if (AppConfigHelper.strIsDataSync == 1)
                {
                    LogServiceHelper.WriteSyncDataLog("开始数据同步服务。。。。。。。。");
                    timer2_sync.Enabled = true;
                    timer2_sync.Interval = (double)AppConfigHelper.strAlertTimer_Sync;
                    timer2_sync.Start();
                    LogServiceHelper.WriteSyncDataLog("数据同步服务成功启动");
                }
                else
                {
                    LogServiceHelper.WriteMessageLog("数据同步不需要");
                }
            }
            catch (Exception ex)
            {
                LogServiceHelper.WriteSyncDataError("数据同步服务成功失败：" + ex.Message, ex);
            }
        }

        private void StartWeChatService()
        {
            try
            {
                if (AppConfigHelper.strIsWeChatSync == 1)
                {
                    LogServiceHelper.WriteSyncDataLog("开始微信推送同步服务。。。。。。。。");
                    timer_wechat.Enabled = true;
                    timer_wechat.Interval = (double)AppConfigHelper.strAlertTimer_WeChat;
                    timer_wechat.Start();
                    LogServiceHelper.WriteSyncDataLog("开始微信推送同步服务成功启动");
                }
                else
                {
                    LogServiceHelper.WriteMessageLog("开始微信推送同步服务不需要");
                }
            }
            catch (Exception ex)
            {
                LogServiceHelper.WriteSyncDataError("开始微信推送同步服务失败：" + ex.Message, ex);
            }
        }

        protected override void OnStop()
        {
            isWC = true;
            timer1.Enabled = false;
            timer1.Stop();
            timer2_sync.Enabled = false;
            timer2_sync.Stop();
            timer_wechat.Enabled = false;
            timer_wechat.Stop();
            LogServiceHelper.WriteMessageLog("停止服务");
            LogServiceHelper.WriteSyncDataLog("停止服务");
            Dispose();
        }

        bool isWC = true;

        private void timer2_sync_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (AppConfigHelper.strIsDataSync == 1)
                {
                    if (isWC)
                    {
                        isWC = false;
                        SyncInfoDataService.SyncDataALL();
                        isWC = true;
                    }
                }
            }
            catch (Exception ex)
            {
                isWC = true;
                LogServiceHelper.WriteSyncDataError("数据同步出现异常错误：" + ex.Message, ex);
            }
        }

        bool isAutoPlay = true;
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (AppConfigHelper.strIsSmsSync == 1)
                {
                    if (isAutoPlay)
                    {
                        isAutoPlay = false;
                        MsgService.SendInstantSMS();
                        MsgService.SendSMS();
                        isAutoPlay = true;
                    }
                }
            }
            catch (Exception ex2)
            {
                isAutoPlay = true;
                LogServiceHelper.WriteMessageError("发送及时短信出现异常错误：" + ex2.Message, ex2);
            }
        }
        
        bool isWeChat = true;
        private void timer_wechat_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (AppConfigHelper.strIsWeChatSync == 1)
                {
                    if (isWeChat)
                    {
                        isWeChat = false;
                        WeChatService.WeChatPushNotice();
                        WeChatService.WeChatPushConsult();
                        isWeChat = true;
                    }
                }
            }
            catch (Exception ex2)
            {
                isWeChat = true;
                LogServiceHelper.WriteMessageError("推送微信内容出现异常错误：" + ex2.Message, ex2);
            }
        }
    }
}
